/*
 * Created by JFormDesigner on Mon Jun 24 15:05:09 CST 2024
 */

package scct.haoyuqing.ui;

import scct.haoyuqing.dao.UserDao;
import scct.haoyuqing.model.User;
import scct.haoyuqing.dao.UserDao;
import scct.haoyuqing.model.User;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * @author le
 */
public class FormLogin extends JFrame {
    public FormLogin() {
        initComponents();
    }

    private void btnLogin(ActionEvent e) throws ClassNotFoundException {
//        System.out.println("按钮点击了");
        //获取文本框的输入的用户名和密码
        String name = txtName.getText();
        String  pass=txtPass.getText();
        System.out.println(name+","+pass);
//        name.matches("正则表达式")
        if (!name.matches("^\\w{3,10}$"))
        {
            JOptionPane.showConfirmDialog(null,"你输入的用户名不合法，请检查后重新输入");
            return;
        }
        if (!pass.matches("^\\w{3,10}$")) {
            JOptionPane.showConfirmDialog(null,"你输入的密码不合法，请检查后输入");
            return;
        }
        UserDao userDao =new UserDao();
        User user=userDao.login(name,pass);
        if (user==null){
            JOptionPane.showConfirmDialog(this,"用户名或密码错误，请检查后登录");
            return;
        }else {
            JOptionPane.showConfirmDialog(this,"登录成功");
            FormMain formMain=new FormMain();
            formMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            formMain.setSize(800,600);
//            formMain.set
            formMain.setVisible(true);
            //进入主窗体


        }

    }




    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - hao
        label1 = new JLabel();
        label2 = new JLabel();
        txtName = new JTextField();
        label3 = new JLabel();
        txtPass = new JTextField();
        btnLogin = new JButton();
        btnReg = new JButton();

        //======== this ========
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- label1 ----
        label1.setText("   \u767b\u5f55");
        label1.setFont(new Font("Microsoft JhengHei Light", Font.PLAIN, 24));
        contentPane.add(label1);
        label1.setBounds(195, 35, 105, 40);

        //---- label2 ----
        label2.setText("\u7528\u6237\u540d");
        label2.setFont(new Font("Adobe \u9ed1\u4f53 Std R", Font.PLAIN, 16));
        contentPane.add(label2);
        label2.setBounds(65, 115, 65, 35);
        contentPane.add(txtName);
        txtName.setBounds(175, 115, 270, 30);

        //---- label3 ----
        label3.setText("\u5bc6\u7801");
        label3.setFont(new Font("Adobe \u9ed1\u4f53 Std R", Font.BOLD, 16));
        contentPane.add(label3);
        label3.setBounds(65, 180, 55, 35);
        contentPane.add(txtPass);
        txtPass.setBounds(180, 185, 265, 30);

        //---- btnLogin ----
        btnLogin.setText("\u767b\u5f55");
        btnLogin.setFont(new Font("Adobe \u5b8b\u4f53 Std L", Font.PLAIN, 18));
        btnLogin.addActionListener(e -> {try {
btnLogin(e);} catch (ClassNotFoundException ex) {
    throw new RuntimeException(ex);
}});
        contentPane.add(btnLogin);
        btnLogin.setBounds(new Rectangle(new Point(110, 280), btnLogin.getPreferredSize()));

        //---- btnReg ----
        btnReg.setText("\u6ce8\u518c");
        btnReg.setFont(new Font("Adobe \u5b8b\u4f53 Std L", Font.PLAIN, 14));
        contentPane.add(btnReg);
        btnReg.setBounds(new Rectangle(new Point(305, 280), btnReg.getPreferredSize()));

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - hao
    private JLabel label1;
    private JLabel label2;
    private JTextField txtName;
    private JLabel label3;
    private JTextField txtPass;
    private JButton btnLogin;
    private JButton btnReg;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}